package com.zookeeper.te1;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * 作者: LCG
 * 日期: 2019/9/17 09:19
 * 描述:
 */
public class ZkLock {

    public static void main(String[] args) throws Exception {

        RetryPolicy retryPolicy=new ExponentialBackoffRetry(1000,3);

        CuratorFramework curatorFramework=CuratorFrameworkFactory.newClient("192.168.137.1:2181",retryPolicy);

        curatorFramework.start();

        InterProcessMutex interProcessMutex=new InterProcessMutex(curatorFramework,"/LOCKS/LOCK");

        interProcessMutex.acquire();//获取锁

        //执行业务

        interProcessMutex.release();//释放锁

        curatorFramework.close();//关闭连接

    }

}
